package com.example.emos.wx.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.example.emos.wx.db.mapper.TbRoleMapper;
import com.example.emos.wx.db.pojo.TbRole;
import com.example.emos.wx.exception.EmosException;
import com.example.emos.wx.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * @ClassName RoleServiceImpl
 * @Description TODO
 * @Author daoketa
 * @Version 1.0
 **/
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    private TbRoleMapper roleMapper;

    /**
     * 获取角色的所有权限
     * @author daoketa
     * @param
     *
     */
    @Override
    public ArrayList<HashMap> searchRoleOwnPermission(Integer roleId) {
        ArrayList<HashMap> list = roleMapper.searchRoleOwnPermission(roleId);
        list = handleData(list);
        return list;
    }

    /**
     * 添加角色
     * @author daoketa
     * @param
     *
     */
    @Override
    public void insertRole(TbRole role) {
        Integer rows = roleMapper.insertRole(role);
        if (rows != 1){
            throw new EmosException("添加角色失败");
        }
    }

    /**
     * 更新角色权限
     * @author daoketa
     * @param
     *
     */
    @Override
    public void updateRolePermission(TbRole role) {
        log.info(role.getDefaultPermissions());
        Integer rows = roleMapper.updateRolePermission(role);
        if (rows != 1) {
            throw new EmosException("修改角色失败");
        }
    }

    /**
     * 查询所有权限
     * @author daoketa
     * @param
     *
     */
    @Override
    public ArrayList<HashMap> searchAllPermission() {
        ArrayList<HashMap> list = roleMapper.searchAllPermission();
        ArrayList<HashMap> result = handleData(list);
        return result;
    }

    /**
     * 查询所有角色
     * @author daoketa
     * @param
     *
     */
    @Override
    public List<TbRole> searchAllRole() {
        List<TbRole> list = roleMapper.searchAllRole();
        return list;
    }

    /**
     * 删除角色，若角色关联着用户，则无法删除
     * @author daoketa
     * @param
     *
     */
    @Override
    public void deleteRoleById(int id) {
        long count = roleMapper.searchRoleUsersCount(id);
        if (count > 0) {
            throw new EmosException("该角色关联着用户，所以无法删除");
        }
        int row = roleMapper.deleteRoleById(id);
        if (row != 1) {
            throw new EmosException("角色删除失败");
        }
    }

    /**
     * 查询所有权限-将查询结果按照模块名称分组
     *
     * @param list
     * @return
     */
    private ArrayList<HashMap> handleRoleOwnPermission(ArrayList<HashMap> list){
        ArrayList<HashMap> result = new ArrayList<>();
        String moduleName = "";
        HashMap map = null;
        JSONArray action = null;
        HashMap actionMap = null;

        for (HashMap one: list) {
            String tempModuleName = one.get("moduleName").toString();
            if (!moduleName.equals(tempModuleName)){
                Integer id = Integer.valueOf(one.get("id").toString());
                moduleName = tempModuleName;
                map = new HashMap();
                action = new JSONArray();
                result.add(map);
                map.put("id", id);
                map.put("moduleName", moduleName);
                map.put("action", action);
            }
            actionMap = new HashMap();
            actionMap.put("actionName",one.get("actionName"));
            actionMap.put("selected",one.get("selected").toString());
            action.add(actionMap);
        }
        return result;
    }

    /**
     * 将查询结果按照模块名称分组
     *
     * @param list
     * @return
     */
    private ArrayList<HashMap> handleData(ArrayList<HashMap> list) {
        ArrayList permsList = new ArrayList();
        ArrayList actionList = new ArrayList();
        HashSet set = new HashSet();
        HashMap data = new HashMap();

        for (HashMap map : list) {
            long permissionId = (Long) map.get("id");
            String moduleName = (String) map.get("moduleName");
            String actionName = (String) map.get("actionName");
            String selected = map.get("selected").toString();
            String acquiescent = map.get("acquiescent").toString();
            if (set.contains(moduleName)) {
                JSONObject json = new JSONObject();
                json.set("id", permissionId);
                json.set("actionName", actionName);
                json.set("selected", selected.equals("1") ? true : false);
                json.set("acquiescent", acquiescent.equals("1") ? true : false);
                actionList.add(json);
            } else {
                set.add(moduleName);
                data = new HashMap();
                data.put("moduleName", moduleName);
                actionList = new ArrayList();
                JSONObject json = new JSONObject();
                json.set("id", permissionId);
                json.set("actionName", actionName);
                json.set("selected", selected.equals("1") ? true : false);
                json.set("acquiescent", acquiescent.equals("1") ? true : false);
                actionList.add(json);
                data.put("action", actionList);
                permsList.add(data);
            }

        }
        return permsList;
    }


}
